#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using raya.Library;
using raya.Models.photos;

namespace raya.Controllers
{
    public class photosController : baseController
    {
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void Delete(string ids)
        {
            if (Session["id"] == null || String.IsNullOrEmpty(ids))
            {
                return;
            }
            string[] imageids;
            if (ids.Contains(","))
            {
                imageids = ids.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                imageids = new[] {ids};
            }
            
            // check that image is currently logged in user
            string userid = Session["id"].ToString();
            foreach (var imageid in imageids)
            {
                if (!photos.isImageBelongToUser(userid, imageid))
                {
                    continue;
                }

                // delete original image
                photos.deleteImageFromDatabaseBasedOnImageID(imageid);
            }
        }

        public ActionResult Upload()
        {
            if (Session["id"] == null)
            {
                return Content("");
            }
            photos thisImage = new photos();
            
            long ticks = DateTime.UtcNow.Ticks;
            if (Session["id"] == null)
            {
                return Content("");
            }
            string userid = Session["id"].ToString();

            var jpegImageUpload = Request.Files["Filedata"];
            string filenameASCII = Utility.ConvertToASCII(jpegImageUpload.FileName);
            
#if AZURE
            var container = AzureBlob.CreateBlobContainer(Img.blobImageContainerName);
            string blobname = Path.GetFileName(filenameASCII);
            var blobproperties = new BlobProperties(blobname) {ContentType = jpegImageUpload.ContentType};
            var blobcontents = new BlobContents(jpegImageUpload.InputStream);
            bool ok = AzureBlob.AddBlobItem(container, blobproperties,blobcontents, false);
            
            thisImage.url = blobname;
            thisImage.userid = userid;
#else
            string folder = "Uploads/photo/" + DateTime.UtcNow.DayOfYear + "/";
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + folder))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + folder);
            }
            thisImage.url = "/" + folder + userid + "_" + ticks + filenameASCII;
            thisImage.userid = int.Parse(userid);
            bool ok = Img.saveImage(jpegImageUpload,thisImage.url);
#endif
            if (!ok)
            {
                return Content("");
            }

            string thumbnailUrl = Img.by_size(thisImage.url, 100, 66);
            
            // save to database
            string id = thisImage.insertImageIntoDatabase();

            return Content(id + "," + thumbnailUrl);
        }

    }
}